package com.hooker.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hooker.bean.FilterInfo
import com.hooker.bean.TypeInfo
import com.hooker.helper.FilterEnum
import com.hooker.listener.OnItemClickListener
import com.hooker.settings.SettingHelper
import com.sdk.hooker.penetration.R

/**
 * filter类别  path,class,method
 */
class TypeFilterAdapter : RecyclerView.Adapter<TypeFilterAdapter.TypeItemHolder> {

    var context: Context? = null
    var list: MutableList<FilterInfo> = mutableListOf()
    var listView: RecyclerView? = null
    lateinit var mClickListener: OnItemClickListener
    var typeInfo: TypeInfo?

    constructor(
        context: Context,
        list: MutableList<FilterInfo>,
        view: RecyclerView,
        info: TypeInfo?
    ) {
        this.context = context
        this.list.addAll(list)
        this.listView = view
        this.typeInfo = info
    }

    fun update(data: MutableList<FilterInfo>) {
        this.list.clear()
        this.list.addAll(data)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeItemHolder {
        var view: View = View.inflate(context, R.layout.layout_item_filter, null)
        view.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        return TypeItemHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: TypeItemHolder, position: Int) {
        var info = list?.get(position)

        holder.name?.text = info?.id

        var tag = SettingHelper.getBuildInfo(
            typeInfo?.packageName,
            typeInfo?.id,
            info?.id,
            FilterEnum.FilterType.INCLUDE.name
        )
        bindViewData(
            holder.editInclude,
            holder.btnInclude,
            holder.viewInclude,
            info.includes, tag
        )
        tag = SettingHelper.getBuildInfo(
            typeInfo?.packageName,
            typeInfo?.id,
            info?.id,
            FilterEnum.FilterType.START.name
        )
        bindViewData(holder.editStart, holder.btnStart, holder.viewStart, info.starts, tag)
        tag = SettingHelper.getBuildInfo(
            typeInfo?.packageName,
            typeInfo?.id,
            info?.id,
            FilterEnum.FilterType.END.name
        )
        bindViewData(holder.editEnd, holder.btnEnd, holder.viewEnd, info.ends, tag)

        tag = SettingHelper.getBuildInfo(
            typeInfo?.packageName,
            typeInfo?.id,
            info?.id,
            FilterEnum.FilterType.REGEX.name
        )
        bindViewData(holder.editRegex, holder.btnRegex, holder.viewRegex, info.regexs, tag)
    }

    private fun bindViewData(
        editView: EditText,
        btnView: Button,
        listView: RecyclerView,
        list: MutableList<String>,
        tag: String
    ) {
        btnView.setOnClickListener { view ->

            var text = editView.text.toString()
            if (text != null && text != "") {
                SettingHelper.putInfo(
                    context, tag, text
                )
                if (!list.contains(text)) {
                    list.add(text)
                }
                var adapter: TypeItemAdapter = listView.adapter as TypeItemAdapter
                adapter?.update(list)

                editView.setText("")
            }
        }
        var adapter = TypeItemAdapter(context, list, listView, object : OnItemClickListener {
            override fun onClick(view: View, position: Int) {
                var text = view?.tag as String
                SettingHelper.removeInfo(
                    context, tag, text
                )
                if (list.contains(text)) {
                    list.remove(text)
                }
                var adapter: TypeItemAdapter = listView.adapter as TypeItemAdapter
                adapter?.removeItem(text)
            }
        })
        listView.adapter = adapter
    }

    class TypeItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var name: TextView? = null
        var editInclude: EditText
        var btnInclude: Button
        var viewInclude: RecyclerView

        var editStart: EditText
        var btnStart: Button
        var viewStart: RecyclerView

        var editEnd: EditText
        var btnEnd: Button
        var viewEnd: RecyclerView

        var editRegex: EditText
        var btnRegex: Button
        var viewRegex: RecyclerView

        init {
            name = itemView.findViewById(R.id.filter_name)
            editInclude = itemView.findViewById(R.id.edit_include)
            btnInclude = itemView.findViewById(R.id.btn_include)
            viewInclude = itemView.findViewById(R.id.include_view)
            viewInclude.layoutManager =
                LinearLayoutManager(itemView.context, RecyclerView.VERTICAL, false)
            viewInclude.overScrollMode = View.OVER_SCROLL_NEVER

            editStart = itemView.findViewById(R.id.edit_start)
            btnStart = itemView.findViewById(R.id.btn_start)
            viewStart = itemView.findViewById(R.id.start_view)
            viewStart.layoutManager =
                LinearLayoutManager(itemView.context, RecyclerView.VERTICAL, false)
            viewStart.overScrollMode = View.OVER_SCROLL_NEVER

            editEnd = itemView.findViewById(R.id.edit_end)
            btnEnd = itemView.findViewById(R.id.btn_end)
            viewEnd = itemView.findViewById(R.id.end_view)
            viewEnd.layoutManager =
                LinearLayoutManager(itemView.context, RecyclerView.VERTICAL, false)
            viewEnd.overScrollMode = View.OVER_SCROLL_NEVER

            editRegex = itemView.findViewById(R.id.edit_end)
            btnRegex = itemView.findViewById(R.id.btn_end)
            viewRegex = itemView.findViewById(R.id.end_view)
            viewRegex.layoutManager =
                LinearLayoutManager(itemView.context, RecyclerView.VERTICAL, false)
            viewRegex.overScrollMode = View.OVER_SCROLL_NEVER
        }
    }
}